package com.yn.xgame.logic.player;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.yn.xgame.logic.GamePlayer;
import com.yn.xgame.logic.util.DateUtil;
import com.yn.xgame.logic.util.MD5Utils;
import com.yn.xgame.message.C2GPbRedirectMessage;
import com.yn.xgame.pb.GateClientProto;
import com.yn.xgame.pb.MessageIdProto.MessageId;

public class UserBankModule {
	private static final BigDecimal MIN = new BigDecimal(100);// 操作最小钱
	private static final int FAILED = 0;
	private static final int SUCCESS = 1;
	
	
	@Override
	public String toString() {
		return "UserBankModule [gamePlayer=" + gamePlayer + ", list=" + list + "]";
	}

	private static final int PWD_CONFLICT = 2;

	private static final int DRAW_ILLEGAL = 3;// 取的钱多于余额
	private static final int DIGITAL_ILLEGAL = 4;// 数字不合法
	private static final int FLOOR_ERROR = 5;// 操作小于MIN

	GamePlayer gamePlayer;
	List<UserBankAssetsLog> list;

	public List<UserBankAssetsLog> getList() {
		return list;
	}

	public void setList(List<UserBankAssetsLog> list) {
		this.list = list;
	}

	public UserBankModule(GamePlayer gamePlayer, List<UserBankAssetsLog> list) {
		super();
		this.gamePlayer = gamePlayer;
		this.list = list;
	}

	// 查询存取款记录
	public void queryBankAssetsLog(C2GPbRedirectMessage msg) {
		GateClientProto.G2CMessage resp = GateClientProto.G2CMessage.newBuilder()
				.setMsgid(MessageId.G2C_UserBankAssetsLogResp)
				.setUserBankAssetsLogResp(GateClientProto.UserBankAssetsLogResp.newBuilder().addAllUserBankAssetsLog(
						list.stream().map(UserBankAssetsLog::build).collect(Collectors.toList())))
				.build();
		gamePlayer.respToClient(msg.getFromGateTopic(), resp);
	}

	// 修改银行密码
	public void updateBankPwd(C2GPbRedirectMessage msg) {
		String warning="";
		int Code = 0 ;
		String oldPwd=msg.getPb().getUpdateUserBankPwdReq().getOldBankPwd();
		String newPwd = msg.getPb().getUpdateUserBankPwdReq().getNewBankPwd();
		if (!gamePlayer.getBankPwd().equals(MD5Utils.encryptTohex(oldPwd))) {
			 Code=FAILED;
			 warning="登录密码不正确";
		}else{
			if (gamePlayer.getPwd().equals(MD5Utils.encryptTohex(newPwd))) {
				Code=FAILED;
				warning="银行密码与登录密码相同";
			}else {
				try {
					gamePlayer.setBankPwd(MD5Utils.encryptTohex(newPwd));
					Code = SUCCESS;
					warning="修改密码成功";
				} catch (Exception e) {
				}
			}
		}
		GateClientProto.G2CMessage resp = GateClientProto.G2CMessage.newBuilder()
				.setMsgid(MessageId.G2C_UpdateUserBankPwdResp)
				.setUpdateUserBankPwdResp(GateClientProto.UpdateUserBankPwdResp.newBuilder()
						.setResultCode(Code))
			    .setResult(Code)
			    .setWarning(warning)
				.build();
		gamePlayer.respToClient(msg.getFromGateTopic(), resp);
	}

	// 添加用户银行密码
	public void addOrLoginUserBank(C2GPbRedirectMessage msg) {
		String warning="";
		int resultCode = FAILED;
		String pwd = msg.getPb().getAddOrLoginBankReq().getBankPwd();
		String key = msg.getPb().getAddOrLoginBankReq().getKey();
		
		GateClientProto.G2CMessage resp =null;
		if ("add".equals(key)) {
			if (pwd != null && !"".equals(pwd)) {
				if (gamePlayer.getPwd().equals(MD5Utils.encryptTohex(pwd))) {
					resultCode = PWD_CONFLICT;
					warning="登录密码与银行密码相同";
				} else {
					gamePlayer.setBankPwd(MD5Utils.encryptTohex(pwd));
					resultCode = SUCCESS;
					warning="添加密码成功";
				}
			}
			resp= GateClientProto.G2CMessage.newBuilder()
					.setMsgid(MessageId.G2C_AddOrLoginUserBankResp)
					.setAddBankPwdResp(GateClientProto.AddBankPwdResp.newBuilder()
							.setResultCode(resultCode))
					.setWarning(warning)
					.build();
		}
		if ("login".equals(key)) {
			if (pwd != null && !"".equals(pwd)) {
				if (!(gamePlayer.getBankPwd()).equals(MD5Utils.encryptTohex(pwd))) {
					resultCode = 0;
					warning="登录密码错误";
				}else {
					resultCode = 1;
					warning="登录成功";
				} 
			}
			resp=GateClientProto.G2CMessage.newBuilder()
					.setMsgid(MessageId.G2C_AddOrLoginUserBankResp)
					.setLoginBankResp(GateClientProto.LoginBankResp.newBuilder()
							.setResultCode(resultCode))
					.setWarning(warning)
					.build();
		}
		gamePlayer.respToClient(msg.getFromGateTopic(), resp);
	}

	// 取款
	public void handleWithDrawMoney(C2GPbRedirectMessage msg) {
		Date updateTime = null;
		int resultCode = FAILED;
		String desc = "";
		String money = msg.getPb().getDrawMoney().getMoney();
		try {
			if (checkMoney(money)) {
				BigDecimal drawMoney = new BigDecimal(money);
				BigDecimal bankGold = new BigDecimal(gamePlayer.getUserBankGold());
				BigDecimal userWalletGold = new BigDecimal(gamePlayer.getUserWalletGold());
				if (drawMoney.compareTo(MIN) == -1) {
					resultCode = FLOOR_ERROR;
					desc = "每次操作不能小于100";
				} else if (drawMoney.compareTo(bankGold) == 1) {
					resultCode = DRAW_ILLEGAL;
					desc="余额不足";
				} else {
					gamePlayer.setUserBankGold(bankGold.subtract(drawMoney).toString());
					gamePlayer.setUserWalletGold(userWalletGold.add(drawMoney).toString());
					UserBankAssetsLog userBankAssetsLog = new UserBankAssetsLog();
					userBankAssetsLog.setUser_id(gamePlayer.getUserId());
					userBankAssetsLog.setAl_type(2);
					userBankAssetsLog.setAl_gold(drawMoney.negate() + "");
					userBankAssetsLog.setAl_bankGold(gamePlayer.getUserBankGold());
					updateTime = DateUtil.yyyyMMddHHmmss.get().parse(DateUtil.getCurrentTime());
					userBankAssetsLog.setAl_created(updateTime);
					list.add(userBankAssetsLog);
					resultCode = SUCCESS;
					desc="银行金币提取操作成功，请查看您的帐号信息";
				}
				
			} else {
				resultCode = DIGITAL_ILLEGAL;
				desc="无效数字";
			}
		} catch (Exception e) {
		}
		GateClientProto.G2CMessage resp = GateClientProto.G2CMessage.newBuilder()
				.setMsgid(MessageId.G2C_DrawMoneyResp)
				.setWarning(desc)
				.setDrawMoneyResp(GateClientProto.DrawMoneyResp.newBuilder()
						.setResultCode(resultCode))
				.setWarning(desc)
				.build();
		gamePlayer.respToClient(msg.getFromGateTopic(), resp);
	}

	// 存款
	public void handleWithDepositMoney(C2GPbRedirectMessage msg) {
		Date updateTime = null;
		String warning="";
		int resultCode = FAILED;
		String money = msg.getPb().getDepositMoney().getMoney();
		try {
			if (checkMoney(money)) {
				BigDecimal DepositMoney = new BigDecimal(money);
				BigDecimal walletGold = new BigDecimal(gamePlayer.getUserWalletGold());
				BigDecimal bankGold = new BigDecimal(gamePlayer.getUserBankGold());
				if (DepositMoney.compareTo(MIN) == -1) {
					resultCode = FLOOR_ERROR;
					warning="每次操作不小于100";
				} else if (DepositMoney.compareTo(walletGold) == 1) {
					resultCode = DRAW_ILLEGAL;
					warning="余额不足";
				} else {
					gamePlayer.setUserBankGold(bankGold.add(DepositMoney).toString());
					gamePlayer.setUserWalletGold(walletGold.subtract(DepositMoney).toString());

					UserBankAssetsLog userBankAssetsLog = new UserBankAssetsLog();
					userBankAssetsLog.setUser_id(gamePlayer.getUserId());
					userBankAssetsLog.setAl_type(2);
					userBankAssetsLog.setAl_gold(DepositMoney + "");
					userBankAssetsLog.setAl_bankGold(gamePlayer.getUserBankGold());
					updateTime = DateUtil.yyyyMMddHHmmss.get().parse(DateUtil.getCurrentTime());
					userBankAssetsLog.setAl_created(updateTime);
					list.add(userBankAssetsLog);
					resultCode = SUCCESS;
					warning="银行金币存款操作成功，请查看您的帐号信息";
				}
			} else {
				resultCode = DIGITAL_ILLEGAL;
				warning="无效数字";
			}
		} catch (Exception e) {
		}
		GateClientProto.G2CMessage resp = GateClientProto.G2CMessage.newBuilder()
				.setMsgid(MessageId.G2C_DepositMoneyResp)
				.setDepositMoneyResp(GateClientProto.DepositMoneyResp.newBuilder()
						.setResultCode(resultCode))
				.setWarning(warning)
				.build();
		gamePlayer.respToClient(msg.getFromGateTopic(), resp);
	}

	// 钱的合法性
	private boolean checkMoney(String money) {
		try {
			return Integer.valueOf(money) > 0;
		} catch (Exception e) {
			return false;
		}
	}

}
